import Vue from 'vue'
import VueRouter from 'vue-router'
import { sharePage } from '../utils/wx'
import { store } from '../store' // 引入vuex

import Entry from "../views/entry"
import Best from '../views/best'
import Brand from '../views/brand'
import Class from '../views/class'
import Life from '../views/life'
import User from '../views/user'
import ProductList from '../views/product-list'
import ProductFilter from '../views/product-filter'
import Category from '../views/category'
import Product from '../views/product'
import UserClass from '../views/user-class'
import ClassView from '../views/class-view'
import ClassCancel from '../views/class-cancel'
import ClassDetail from '../views/class-detail'
import Score from '../views/score'
import ScoreSend from '../views/score-send'
import Benefit from '../views/benefit'
import Shop from '../views/shop'
import ActivePub from '../views/active-pub'
import ApplyList from '../views/apply-list'
import LifePub from '../views/life-pub'
import LifeDetail from '../views/life-detail'
import RegisterEmail from '../views/register-email'
import RegisterPhone from '../views/register-phone'
import RegisterPassword from '../views/register-password'
import RegisterValidate from '../views/register-validate'
import RegnewPhone from '../views/regnew-phone'
import RegnewShop from '../views/regnew-shop'
import ShopApplying from '../views/shop-applying'
import RegnewEmail from '../views/regnew-email'
import Forget from '../views/forget'
import Login from '../views/login'
import LoginWechat from '../views/login-wechat'
import Search from '../views/search'
import UserBase from '../views/user-base'
import ShopBase from '../views/shop-base'
import UserNick from '../views/user-nick'
import Belong from '../views/belong'
import NewsDetail from '../views/news-detail'
import ClassApply from '../views/class-apply'
import Success from '../views/success'
import Discount from '../views/discount'
import BelongApply from '../views/belong-apply'
import MyCoupon from '../views/mycoupon'
import Collection from '../views/collection'
import History from '../views/history'
import Notice from '../views/notice'
import Address from '../views/address'
import Ticket from "../views/ticket"
import Coupon from "../views/coupon"
import Payment from "../views/payment"
import PaymentAdd from "../views/payment-add"
import Cart from "../views/cart"
import Orders from "../views/orders"
import Order from "../views/order"
import OrdersRefund from "../views/orders-refund"
import OrderRefund from "../views/order-refund"
import OrderConfirm from "../views/order-confirm"
import Express from "../views/express"
import BenefitWithdraw from "../views/benefit-withdraw"
import Fans from "../views/fans"
import FansLife from "../views/fans-life"
import Comments from "../views/comments"
import Comment from "../views/comment"
import Pay from "../views/pay"
import Test from "../views/test"
import TestPay from "../views/test-pay"
import TestVantlist from "../views/test-vantlist"
import TestCopy from "../views/test-copy"
import ActiveSale from "../views/active-sale"
import ReturnMoney from "../views/return-money"
import ReturnProduct from "../views/return-product"
import LoginAuto from "../views/login-auto"
import ToAlipay from "../views/toalipay/index"

Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    redirect: "/best",
  },
  {
    path: "/entry",
    name: "Entry",
    component: Entry,
  },
  {
    path: "/best",
    name: "Best",
    component: Best,
    meta: {
      keepAlive: true,
    },
  },
  {
    path: "/brand",
    name: "Brand",
    component: Brand,
    meta: {
      keepAlive: true,
    },
  },
  {
    path: "/class",
    name: "Class",
    component: Class,
  },
  {
    path: "/life",
    name: "Life",
    component: Life,
    meta: {
      keepAlive: true,
    },
  },
  {
    path: "/user",
    name: "User",
    component: User,
  },
  {
    path: "/productList",
    name: "ProductList",
    component: ProductList,
    meta: {
      keepAlive: true,
    },
  },
  {
    path: "/productFilter",
    name: "ProductFilter",
    component: ProductFilter,
  },
  {
    path: "/category",
    name: "Category",
    component: Category,
  },
  {
    path: "/product",
    name: "Product",
    component: Product,
  },
  {
    path: "/userClass",
    name: "UserClass",
    component: UserClass,
  },
  {
    path: "/classView",
    name: "ClassView",
    component: ClassView,
  },
  {
    path: "/classCancel",
    name: "ClassCancel",
    component: ClassCancel,
  },
  {
    path: "/classDetail",
    name: "ClassDetail",
    component: ClassDetail,
  },
  {
    path: "/score",
    name: "Score",
    component: Score,
  },
  {
    path: "/scoreSend",
    name: "ScoreSend",
    component: ScoreSend,
  },
  {
    path: "/benefit",
    name: "Benefit",
    component: Benefit,
  },
  {
    path: "/shop",
    name: "Shop",
    component: Shop,
  },
  {
    path: "/activePub",
    name: "ActivePub",
    component: ActivePub,
  },
  {
    path: "/applyList",
    name: "ApplyList",
    component: ApplyList,
  },
  {
    path: "/lifePub",
    name: "LifePub",
    component: LifePub,
  },
  {
    path: "/lifeDetail",
    name: "LifeDetail",
    component: LifeDetail,
  },
  {
    path: "/registerEmail",
    name: "RegisterEmail",
    component: RegisterEmail,
  },
  {
    path: "/registerPhone",
    name: "RegisterPhone",
    component: RegisterPhone,
  },
  {
    path: "/registerValidate",
    name: "RegisterValidate",
    component: RegisterValidate,
  },
  {
    path: "/registerPassword",
    name: "RegisterPassword",
    component: RegisterPassword,
  },
  {
    path: "/regnewPhone",
    name: "RegnewPhone",
    component: RegnewPhone,
  },
  {
    path: "/regnewShop",
    name: "RegnewShop",
    component: RegnewShop,
  },
  {
    path: "/shopApplying",
    name: "ShopApplying",
    component: ShopApplying,
  },
  {
    path: "/regnewEmail",
    name: "RegnewEmail",
    component: RegnewEmail,
  },
  {
    path: "/forget",
    name: "Forget",
    component: Forget,
  },
  {
    path: "/login",
    name: "Login",
    component: Login,
  },
  {
    path: "/loginWechat",
    name: "LoginWechat",
    component: LoginWechat,
  },
  {
    path: "/search",
    name: "Search",
    component: Search,
    meta: {
      keepAlive: true,
    },
  },
  {
    path: "/userBase",
    name: "UserBase",
    component: UserBase,
  },
  {
    path: "/shopBase",
    name: "ShopBase",
    component: ShopBase,
  },
  {
    path: "/userNick",
    name: "UserNick",
    component: UserNick,
  },
  {
    path: "/belong",
    name: "Belong",
    component: Belong,
  },
  {
    path: "/newsDetail",
    name: "NewsDetail",
    component: NewsDetail,
  },
  {
    path: "/classApply",
    name: "ClassApply",
    component: ClassApply,
  },
  {
    path: "/success",
    name: "Success",
    component: Success,
  },
  {
    path: "/discount",
    name: "Discount",
    component: Discount,
  },
  {
    path: "/belongApply",
    name: "BelongApply",
    component: BelongApply,
  },
  {
    path: "/myCoupon",
    name: "MyCoupon",
    component: MyCoupon,
  },
  {
    path: "/collection",
    name: "Collection",
    component: Collection,
  },
  {
    path: "/history",
    name: "History",
    component: History,
  },
  {
    path: "/notice",
    name: "Notice",
    component: Notice,
  },
  {
    path: "/address",
    name: "Address",
    component: Address,
  },
  {
    path: "/ticket",
    name: "Ticket",
    component: Ticket,
  },
  {
    path: "/coupon",
    name: "Coupon",
    component: Coupon,
  },
  {
    path: "/payment",
    name: "Payment",
    component: Payment,
  },
  {
    path: "/paymentAdd",
    name: "PaymentAdd",
    component: PaymentAdd,
  },
  {
    path: "/cart",
    name: "Cart",
    component: Cart,
  },
  {
    path: "/orders",
    name: "Orders",
    component: Orders,
  },
  {
    path: "/order",
    name: "Order",
    component: Order,
  },
  {
    path: "/ordersRefund",
    name: "OrdersRefund",
    component: OrdersRefund,
  },
  {
    path: "/orderRefund",
    name: "OrderRefund",
    component: OrderRefund,
  },
  {
    path: "/orderConfirm",
    name: "OrderConfirm",
    component: OrderConfirm,
  },
  {
    path: "/express",
    name: "Express",
    component: Express,
  },
  {
    path: "/benefitWithdraw",
    name: "BenefitWithdraw",
    component: BenefitWithdraw,
  },
  {
    path: "/fans",
    name: "Fans",
    component: Fans,
  },
  {
    path: "/fansLife",
    name: "FansLife",
    component: FansLife,
  },
  {
    path: "/comments",
    name: "Comments",
    component: Comments,
  },
  {
    path: "/comment",
    name: "Comment",
    component: Comment,
  },
  {
    path: "/pay",
    name: "Pay",
    component: Pay,
  },
  {
    path: "/test",
    name: "Test",
    component: Test,
  },
  {
    path: "/testpay",
    name: "TestPay",
    component: TestPay,
  },
  {
    path: "/testvantlist",
    name: "TestVantlist",
    component: TestVantlist,
  },
  {
    path: "/testcopy",
    name: "TestCopy",
    component: TestCopy,
  },
  {
    path: "/activesale",
    name: "ActiveSale",
    component: ActiveSale,
  },
  {
    path: "/returnmoney",
    name: "ReturnMoney",
    component: ReturnMoney,
  },
  {
    path: "/returnProduct",
    name: "ReturnProduct",
    component: ReturnProduct,
  },
  {
    path: "/loginauto",
    name: "LoginAuto",
    component: LoginAuto,
  },
  {
    path: "/toalipay",
    name: "ToAlipay",
    component: ToAlipay,
  },
]

const router = new VueRouter({
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  },
})

// 导航守卫 滚动条始终回到页面顶部
router.beforeEach((to, from, next) => {
  // 只针对返回页面缓存
  if (
    (to.name === "Product" && from.name === "Best") ||
    (to.name === "Discount" && from.name === "Best") ||
    (to.name === "Product" && from.name === "ProductList") ||
    (to.name === "Product" && from.name === "Search") ||
    (to.name === "NewsDetail" && from.name === "Brand") ||
    (to.name === "LifeDetail" && from.name === "Life")
  ) {
    from.meta.keepAlive = true
  } else {
    from.meta.keepAlive = false
  }
  
  // 设置分享
  sharePage()
  
  // setTimeout(() => {
    // console.log('滚动条位置: ', document.documentElement.scrollTop)
    // document.documentElement.scrollTop = 0
  // }, 2000);

  // 取消请求
  store.commit('clearToken') 
  next()
  
})

export default router
